package com.kelp.codetoolkit.utils

import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.PsiManager
import java.io.File

/**
 * @author: liuyuebai
 * @date: 2024/11/1 20:39
 * @description:
 */
object HandleFileUtil {

    fun getPsiJavaFile(path: String,project: Project): PsiJavaFile? {
        val psiFile = getPsiFile(path, project)
        if (null != psiFile && psiFile is PsiJavaFile) {
            return psiFile
        }
        return null
    }

    fun getPsiFile(path: String,project: Project): PsiFile? {
        val psiManager = PsiManager.getInstance(project)
        val virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(File(path))
        if (null != virtualFile) {
            val findFile = psiManager.findFile(virtualFile)
            return findFile
        }
        return null
    }
}